<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>打家劫舍</title>
</head>
<body>
  <script>
    function fn(nums) {
      let len = nums.length;
      if (len == 0) return 0;
      let dp = new Array(len + 1);
      dp[0] = 0;
      dp[1] = nums[0];
      for (let i = 2; i <= len; i++) {
        dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
      }
      return dp[len];
    }
    console.log(fn([1, 2, 3, 1]))
  </script>
</body>
</html>